Merge branch 'master' of github.com:cantino/huginn

Andrew Cantino 9 gadi atpakaļ
vecāks
revīzija
46a141158c

+ 16 - 9
app/models/agents/twitter_user_agent.rb

@@ -5,13 +5,15 @@ module Agents
5 5
     cannot_receive_events!
6 6
 
7 7
     description <<-MD
8
-      The Twitter User Agent follows the timeline of a specified Twitter user.
8
+      The Twitter User Agent either follows the timeline of a specific Twitter user or follows your own home timeline including both your tweets and tweets from people whom you are following.
9 9
 
10 10
       #{twitter_dependencies_missing if dependencies_missing?}
11 11
 
12 12
       To be able to use this Agent you need to authenticate with Twitter in the [Services](/services) section first.
13 13
 
14
-      You must also provide the `username` of the Twitter user to monitor.
14
+      To follow a Twitter user set `choose_home_time_line` to `false` and provide the `username`.
15
+
16
+      To follow your own home timeline set `choose_home_time_line` to `true`.
15 17
 
16 18
       Set `include_retweets` to `false` to not include retweets (default: `true`)
17 19
       
@@ -24,7 +26,6 @@ module Agents
24 26
 
25 27
     event_description <<-MD
26 28
       Events are the raw JSON provided by the [Twitter API](https://dev.twitter.com/docs/api/1.1/get/statuses/user_timeline). Should look something like:
27
-
28 29
           {
29 30
              ... every Tweet field, including ...
30 31
             "text": "something",
@@ -57,13 +58,14 @@ module Agents
57 58
         'username' => 'tectonic',
58 59
         'include_retweets' => 'true',
59 60
         'exclude_replies' => 'false',
60
-        'expected_update_period_in_days' => '2'
61
+        'expected_update_period_in_days' => '2',
62
+        'choose_home_time_line' => 'false'
61 63
       }
62 64
     end
63 65
 
64 66
     def validate_options
65
-      errors.add(:base, "username is required") unless options['username'].present?
66 67
       errors.add(:base, "expected_update_period_in_days is required") unless options['expected_update_period_in_days'].present?
68
+      errors.add(:base, "username is required") if options['username'].blank? && !boolify(options['choose_home_time_line'])
67 69
 
68 70
       if options[:include_retweets].present? && !%w[true false].include?(options[:include_retweets])
69 71
         errors.add(:base, "include_retweets must be a boolean value string (true/false)")
@@ -82,6 +84,10 @@ module Agents
82 84
       end
83 85
     end
84 86
 
87
+    def choose_home_time_line?
88
+      boolify(interpolated['choose_home_time_line'])
89
+    end
90
+
85 91
     def include_retweets?
86 92
       interpolated[:include_retweets] != "false"
87 93
     end
@@ -95,8 +101,11 @@ module Agents
95 101
       opts = {:count => 200, :include_rts => include_retweets?, :exclude_replies => exclude_replies?, :include_entities => true, :contributor_details => true}
96 102
       opts.merge! :since_id => since_id unless since_id.nil?
97 103
 
98
-      # http://rdoc.info/gems/twitter/Twitter/REST/Timelines#user_timeline-instance_method
99
-      tweets = twitter.user_timeline(interpolated['username'], opts)
104
+      if choose_home_time_line?
105
+        tweets = twitter.home_timeline(opts)
106
+      else
107
+        tweets = twitter.user_timeline(interpolated['username'], opts)
108
+      end
100 109
 
101 110
       tweets.each do |tweet|
102 111
         if tweet.created_at >= starting_at
@@ -105,8 +114,6 @@ module Agents
105 114
           create_event :payload => tweet.attrs
106 115
         end
107 116
       end
108
-
109
-      save!
110 117
     end
111 118
   end
112 119
 end

+ 35 - 1
spec/models/agents/twitter_user_agent_spec.rb

@@ -3,7 +3,7 @@ require 'rails_helper'
3 3
 describe Agents::TwitterUserAgent do
4 4
   before do
5 5
     # intercept the twitter API request for @tectonic's user profile
6
-    stub_request(:any, /tectonic/).to_return(:body => File.read(Rails.root.join("spec/data_fixtures/user_tweets.json")), :status => 200)
6
+    stub_request(:any, "https://api.twitter.com/1.1/statuses/user_timeline.json?contributor_details=true&count=200&exclude_replies=false&include_entities=true&include_rts=true&screen_name=tectonic").to_return(:body => File.read(Rails.root.join("spec/data_fixtures/user_tweets.json")), :status => 200)
7 7
 
8 8
     @opts = {
9 9
       :username => "tectonic",
@@ -42,4 +42,38 @@ describe Agents::TwitterUserAgent do
42 42
     end
43 43
   end
44 44
 
45
+  describe "#check that if choose time line is false then username is required" do
46
+    before do
47
+      stub_request(:any, "https://api.twitter.com/1.1/statuses/home_timeline.json?contributor_details=true&count=200&exclude_replies=false&include_entities=true&include_rts=true").to_return(:body => File.read(Rails.root.join("spec/data_fixtures/user_tweets.json")), :status => 200)
48
+    end
49
+
50
+    it 'requires username unless choose_home_time_line is true' do
51
+      expect(@checker).to be_valid
52
+
53
+      @checker.options['username'] = nil
54
+      expect(@checker).to_not be_valid
55
+
56
+      @checker.options['choose_home_time_line'] = 'true'
57
+      expect(@checker).to be_valid
58
+    end
59
+
60
+    context "when choose_home_time_line is true" do
61
+      before do
62
+        @checker.options['choose_home_time_line'] = true
63
+        @checker.options.delete('username')
64
+        @checker.save!
65
+      end
66
+    end
67
+
68
+    it "error messaged added if choose_home_time_line is false and username does not exist" do
69
+
70
+      opts = @opts.tap { |o| o.delete(:username) }.merge!({:choose_home_time_line => "false" })
71
+
72
+      checker = Agents::TwitterUserAgent.new(:name => "tectonic", :options => opts)
73
+      checker.service = services(:generic)
74
+      checker.user = users(:bob)
75
+      expect(checker.save).to eq false
76
+      expect(checker.errors.full_messages.first).to eq("username is required")
77
+    end
78
+  end
45 79
 end